<style>
    .deptall{cursor: pointer;}
    .xuanding{background-color: #f0f6ff;color: #0079fe}
    .dtree-icon-weibiaoti5{color: #f6b918}
    .dtree-icon-dian{color: #FFFFFF!important;}
    .dtree-nav-item{padding-left: 10px!important;}
    /*.dtree-nav-div{overflow: hidden;white-space: nowrap;text-overflow: ellipsis;}*/
    .dtree-nav-div{padding-top: 6px;padding-bottom: 6px;line-height: 18px}
    .disdd{color:#ccc!important}
    .dtree-nav-show{margin-left: 6px;}

    .dtree-toolbar .layui-nav-child{padding: 0 15px}
    .dtree-toolbar .layui-nav-child dd{border-bottom: 1px solid #eee;}
    .dtree-toolbar .layui-nav-child dd a{margin: 4px 0;padding: 2px 0;}
</style>
<div class="layui-col-md3" id="engindao">
    <div class="engindao_bac">
        <div style="height: 50px;line-height: 50px;width: 120px;float: left">
            <a class="deptall deptmovedown" dtree-id="deptTree" dtree-menu="moveDown" style="margin-left: 15px">展开</a>
            <a class="deptall deptmoveup" dtree-id="deptTree" dtree-menu="moveUp" style="margin-left: 15px">收起</a>
        </div>
        <div style="width: calc(100% - 120px);float: left">
            <input type="text" id="deptkeyword" onkeydown="keyTableSearch()" style="height: 30px;width: calc(100% - 40px);float: left;margin-top: 10px" placeholder="输入文件夹名称" autocomplete="off" class="layui-input">
            <button style="height: 30px;line-height: 30px;padding: 0 4px;float: left;margin-top: 10px" type="button" id="deptseabutton" class="layui-btn"><i class="layui-icon layui-icon-search"></i></button>
        </div>
        <div style="clear: both"></div>
        <div class="engindao_div" style="background-color: #f5f5f5;width: calc(100% - 20px);margin-left: 10px;border-radius: 5px">
            <div class="engindao_title"><b>文件夹</b></div>
            <div class="engindao_nav">
                <a id="engindao_left"><i class="layui-icon layui-icon-left"></i></a>
            </div>
        </div>
        <div style="overflow: auto;padding-right: 10px" id="deptToolbarDiv">
            <ul id="deptTree" class="dtree" data-id="0"></ul>
        </div>
    </div>
</div>
<div class="layui-col-md2 layui-hide" id="engindaomin">
    <div class="engindao_bg">
        <div class="col" id="engindao_right"><a><i class="layui-icon layui-icon-right"></i></a></div>
        <div class="engindao_title">文件夹</div>
    </div>
</div>
<script>

    var pub_muArr;
    var param_enginid = getQueryVariable('id') || 0;

    $("#engindao #engindao_left").click(function () {
        var cliHeight = document.getElementById('engindao').clientHeight;

        $("#engindao").addClass('layui-hide');
        $("#table-card #table_list").addClass('engindaomin_guan');

        document.getElementById('engindaomin').style.height = cliHeight+"px";
        $("#engindaomin").removeClass('layui-hide');
    });
    $("#engindaomin #engindao_right").click(function () {
        $("#engindao").removeClass('layui-hide');
        $("#table-card #table_list").removeClass('engindaomin_guan');
        $("#engindaomin").addClass('layui-hide');
    });


    get_mu_info();

    function get_mu_info(muid=0, zhanid=0) {
        layui.use(['dtree','layer','jquery','form','laytp','table','dropdown'],function() {
            var dtree = layui.dtree, layer = layui.layer, $ = layui.jquery, form = layui.form, table = layui.table;
            DeptTree = dtree.render({
                elem: "#deptTree",
                url: server_api_name+ask_enginbaoku_mu_list,
                request: {token: facade.getCookie('token'),enginid:param_enginid,muid:muid},
                width: '100%',
                height: document.documentElement.clientHeight*0.75-60,
                line: true,
                skin: "zdy",
                initLevel: 2,
                nodeIconArray:[
                    {"open": "dtree-icon-weibiaoti5", "close": "dtree-icon-weibiaoti5"},	//文件夹(二级图标默认样式)
                ],
                scroll:"#deptToolbarDiv",
                toolbar:true,
                toolbarWay:'contextmenu',
                toolbarShow:[""],
                toolbarFun:{loadToolbarBefore:function(buttons, param, $div){
                        return setToolMenu(buttons, param, $div);
                    }},
                toolbarExt:[
                    {'title':'新建下级','toolbarId':'addmu',handler:function (data, obj) {
                            openPageEdit('addmu.php?close=1&enginid='+param_enginid+'&pid='+data.basicData.id, '1000px', '690px');
                        }},
                    {'title':'编辑','toolbarId':'editmu',handler:function (data, obj) {
                            openPageEdit('editmu.php?close=1&enginid='+param_enginid+'&id='+data.basicData.id, '1000px', '690px');
                        }},
                    {'title':'复制','toolbarId':'copymu',handler:function (data, obj) {
                            $("#copyid").val(data.basicData.id);
                            $("#copyid").attr('datatype','copy');
                        }},
                    {'title':'剪切','toolbarId':'cutmu',handler:function (data, obj) {
                            $("#copyid").val(data.basicData.id);
                            $("#copyid").attr('datatype','cut');
                        }},
                    {'title':'粘贴','toolbarId':'pastemu',handler:function (data, obj) {
                            var copyid = $("#copyid").val() || 0;
                            var datatype = $("#copyid").attr('datatype') || '';
                            facade.ajax({url: ask_engin_mu_paste, data: {copyid:copyid,datatype:datatype,pasteid:data.basicData.id}}).done(function(res){
                                if (res.code === 2000) {
                                    $("#copyid").val('');
                                    $("#copyid").attr('datatype', '');
                                    facade.success("操作成功!");
                                    setTimeout(() => {
                                        get_mu_info(data.basicData.id);
                                    }, 1500);
                                }
                            });
                        }},
                    {'title':'上传模板','toolbarId':'chuan_mu',handler:function (data, obj) {
                            $("#upload_muid").val(data.basicData.id);
                            $("#upload_muid").attr('datatype', 'muban');
                            document.getElementById('uploadfile').value = null;
                            $("#uploadfile").click();
                        }},
                    {'title':'上移','toolbarId':'shang_mu',handler:function (data, obj) {
                            facade.ajax({url: ask_engin_mu_shang, data: {id:data.basicData.id}}).done(function(res){
                                if (res.code === 2000) {
                                    facade.success("操作成功!");
                                    setTimeout(() => {get_mu_info(data.basicData.pid);}, 1500);
                                }
                            });
                        }},
                    {'title':'下移','toolbarId':'xia_mu',handler:function (data, obj) {
                            facade.ajax({url: ask_engin_mu_xia, data: {id:data.basicData.id}}).done(function(res){
                                if (res.code === 2000) {
                                    facade.success("操作成功!");
                                    setTimeout(() => {get_mu_info(data.basicData.pid);}, 1500);
                                }
                            });
                        }},
                    {'title':'删除','toolbarId':'del_mu',handler:function (data, obj) {
                        delete_temp_cur(ask_engin_mu_del, {id:data.basicData.id}, data.basicData.pid);
                    }},
                    {'title':'详细信息','toolbarId':'show_mu',handler:function (data, obj) {
                        openPageEdit('showmu.php?close=1&id='+data.basicData.id);
                    }},
                ],
                menubar:true,
                menubarTips:{group:[]},
                success: function(data, obj, first){
                    user_token_roletype = data.token_roletype;
                    butauth = data.baoku_butauth;

                    if(check_caoauth('guanjia')){
                        $("#but_guanjia").removeClass('butmenuhide');
                    }else{
                        $("#but_guanjia").addClass('butmenuhide');
                    }
                    if(check_caoauth('chuanbao')){
                        $("#but_chuanbao").removeClass('butmenuhide');
                    }else{
                        $("#but_chuanbao").addClass('butmenuhide');
                    }




                    $("#temp_name").text(data.data[0].basicData.name)

                    pub_muArr = getIdByChildren(data.data, []);
                    if(muid){
                        choose_muid(muid)
                    }else{
                        setWenList(data.data[0].basicData)
                        $("#muid").val(data.data[0].basicData.id)
                        $("#muid_name").val(data.data[0].basicData.parentsName)
                        $("#gofan").attr('pid',data.data[0].basicData.pid);
                        setTimeout(function () {
                            $(".dtree-nav-div[data-id='"+data.data[0].basicData.id+"']").addClass('xuanding');
                        },100)
                    }


                }
            });
            //左键单击事件
            dtree.on("node('deptTree')" ,function(obj){
                $(".dtree-nav-div").removeClass('xuanding');
                $(obj.dom).addClass('xuanding');

                var paramdata = obj.param.basicData;
                $("#muid_name").val(paramdata.parentsName);
                $("#gofan").attr('pid',paramdata.pid);

                setWenList(paramdata)
            });
            //搜索部门
            $("#deptseabutton").click(function(){
                var keyword = $("#deptkeyword").val();
                DeptTree.fuzzySearch(keyword);
            });

        });
    }

    function keyTableSearch() {
        if (event.keyCode == 13) {
            var keyword = $("#deptkeyword").val();
            DeptTree.fuzzySearch(keyword);
        }
    }



    function getIdByChildren(data, Arr) {
        for (var key in data){
            if(data[key].basicData){
                Arr.push(data[key].basicData);
            }
            if(data[key].children){
                getIdByChildren(data[key].children, Arr);
            }
        }
        return Arr;
    }


    function choose_muid(muid) {
        for (var key in pub_muArr) {
            if(pub_muArr[key].id == muid){
                setWenList(pub_muArr[key])
                $("#muid").val(pub_muArr[key].id)
                $("#muid_name").val(pub_muArr[key].parentsName)
                $("#gofan").attr('pid',pub_muArr[key].pid);
            }
        }
        $(".dtree-nav-div").removeClass('xuanding');
        setTimeout(function () {
            $(".dtree-nav-div[data-id='"+muid+"']").addClass('xuanding');
        },100)
    }














    function setToolMenu(buttons, param, $div) {
        if(check_caoauth('rightmenu')){
            $(".dtree-toolbar").removeClass('butmenuhide');
        }else{
            $(".dtree-toolbar").addClass('butmenuhide');
        }



        // "<dd><a dtree-tool='addmu'><i class='dtreefont undefined'></i>&nbsp;新建下级</a></dd>"

        var copyid = $("#copyid").val() || 0;
        var datatype = $("#copyid").attr('datatype') || '';
        if(!copyid || !datatype){
            buttons.pastemu = "<dd><a class='disdd'><i></i>&nbsp;粘贴</a></dd>"
        }

        if(param.basicData.ismu > 0){
            buttons.addmu = "<dd><a class='disdd'><i></i>&nbsp;新建下级</a></dd>"
            buttons.editmu = "<dd><a class='disdd'><i></i>&nbsp;编辑</a></dd>"
            buttons.copymu = "<dd><a class='disdd'><i></i>&nbsp;复制</a></dd>"
            buttons.cutmu = "<dd><a class='disdd'><i></i>&nbsp;剪切</a></dd>"
            buttons.chuan_mu = "<dd><a class='disdd'><i></i>&nbsp;上传模板</a></dd>"
            buttons.shang_mu = "<dd><a class='disdd'><i></i>&nbsp;上移</a></dd>"
            buttons.xia_mu = "<dd><a class='disdd'><i></i>&nbsp;下移</a></dd>"
            buttons.del_mu = "<dd><a class='disdd'><i></i>&nbsp;删除</a></dd>"
            return buttons
        }



        if(param.basicData.pid > 0){
            return buttons;
        }

        buttons.editmu = "<dd><a class='disdd'><i></i>&nbsp;编辑</a></dd>"
        buttons.copymu = "<dd><a class='disdd'><i></i>&nbsp;复制</a></dd>"
        buttons.cutmu = "<dd><a class='disdd'><i></i>&nbsp;剪切</a></dd>"
        buttons.chuan_mu = "<dd><a class='disdd'><i></i>&nbsp;上传模板</a></dd>"
        buttons.chuan_bao = "<dd><a class='disdd'><i></i>&nbsp;上传文件</a></dd>"
        buttons.shang_mu = "<dd><a class='disdd'><i></i>&nbsp;上移</a></dd>"
        buttons.xia_mu = "<dd><a class='disdd'><i></i>&nbsp;下移</a></dd>"
        buttons.del_mu = "<dd><a class='disdd'><i></i>&nbsp;删除</a></dd>"
        return buttons
    }


    function delete_temp_cur(url, data, pid){
        console.log(pid)
        layer.confirm("<div class='tit'>是否确认删除？</div><div class='exp'>删除后不可恢复，请谨慎删除</div>", {
                title:'操作确认',
                icon: 3,
                area:['450px','220px'],
                btn:['确定']
            }, function(conindex){
                facade.ajax({url: url, data:data}).done(function(res){
                    if (res.code === 2000) {
                        facade.success("提交成功!");
                        setTimeout(() => {
                            get_mu_info(pid);
                            layer.close(conindex);
                        }, 1500);
                    }else{
                        layer.close(conindex);
                    }
                });
            }
        )
    }



    function check_caoauth(butname) {
        var res = false;
        for(var key in butauth){
            if(butauth[key].butname == butname){
                res = true;
            }
        }
        return res;
    }

</script>
